package com.example.wlzz.controller.admin;
import com.example.wlzz.constant.CookiesConstant;
import com.example.wlzz.constant.RedisConstant;
import com.example.wlzz.entity.Banner;
import com.example.wlzz.enums.Del_flagEnum;
import com.example.wlzz.repository.BannerRepository;
import com.example.wlzz.repository.mapper.admin.RoleMapper;
import com.example.wlzz.repository.mapper.admin.SmsMapper;
import com.example.wlzz.repository.mapper.admin.UserMapper;
import com.example.wlzz.utils.CookieUtil;
import com.mongodb.client.MongoCollection;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@RestController
@RequestMapping("/backstage")
@Slf4j
@ApiIgnore
public class AdminForPage {
    //懒得写service
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SmsMapper smsMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private BannerRepository bannerRepository;
    @GetMapping("/admin_list")
    public ModelAndView admin_list(){
        return new ModelAndView("/admin-list");
    }
    @GetMapping("/admin_role")
    public ModelAndView admin_role(){
        return new ModelAndView("/admin-role");
    }
    @GetMapping("/admin_cate")
    public ModelAndView admin_cate(){
        return new ModelAndView("/admin-cate");
    }
    @GetMapping("/admin_rule")
    public ModelAndView admin_rule(){
        return new ModelAndView("/admin-rule");
    }
    @GetMapping("/index")
    public ModelAndView index(){
        return new ModelAndView("/index");
    }
    @GetMapping("/member_list")
    public ModelAndView member_list(Map<String,Object> map) {
        map.put("user_number",userMapper.user_number());
        return new ModelAndView("/member-list", map);
    }
    @GetMapping("/banner_add")
    public ModelAndView banner_add(@RequestParam(value = "banner_id",defaultValue = "undefined") String banner_id, Map<String,Object> map) {
        return new ModelAndView("/banner-add", map);
    }

    @GetMapping("/unicode")
    public ModelAndView unicode() {
        return new ModelAndView("/unicode");
    }
    @GetMapping("/all_user_task")
    public ModelAndView all_user_task(Map<String,Object> map) {
        Document existDocument = new Document();
        existDocument.put("delFlag", Del_flagEnum.EXIST.getCode());
        Document deleteDocument = new Document();
        deleteDocument.put("delFlag", Del_flagEnum.DELETE.getCode());
        MongoCollection<Document> taskCollections =  mongoTemplate.getCollection("task");
        map.put("exist_number",taskCollections.count(existDocument));
        map.put("delete_number",taskCollections.count(deleteDocument));
        return new ModelAndView("/all_user_task");
    }
    @GetMapping("/banner")
    public ModelAndView banner(Map<String,Object> map){
        List<Banner> bannerList = bannerRepository.findByFlag(Del_flagEnum.EXIST.getCode());
        map.put("bannerList",bannerList);
        return new ModelAndView("/banner",map);
    }
    @GetMapping("/admin_add")
    public ModelAndView admin_add(){
        return new ModelAndView("/admin-add");
    }
    @GetMapping("/user_task")
    public ModelAndView user_task(@RequestParam("user_id")int userid,Map<String,Object> map) {
        map.put("user_id",userid);
        MongoCollection<Document> taskColleections =  mongoTemplate.getCollection("task");
        Document existDocument = new Document();
        existDocument.put("delFlag", Del_flagEnum.EXIST.getCode());
        existDocument.put("userid",userid);
        Document deleteDocument = new Document();
        deleteDocument.put("delFlag", Del_flagEnum.DELETE.getCode());
        deleteDocument.put("userid",userid);
        map.put("exist_number",taskColleections.count(existDocument));
        map.put("delete_number",taskColleections.count(deleteDocument));
        return new ModelAndView("/user_task",map);
    }
    @GetMapping("/welcome")
    public ModelAndView welcome(HttpServletRequest request, Map<String,Object> map){
        Cookie cookie =  CookieUtil.get(request, CookiesConstant.ADMIN_TOKEN);
        String tokenValue = redisTemplate.opsForValue().get(String.format(RedisConstant.ADMIN_TOKEN_PREFIX, cookie.getValue()));
     //   System.err.println( roleMapper.getRoleName(tokenValue).getRole_name());
        map.put("role", roleMapper.getRoleName(tokenValue));
        Properties sysProperty=System.getProperties(); //系统属性
        map.put("javaVersion",sysProperty.getProperty("java.version"));//Java的运行环境版本
        map.put("javaVmName",sysProperty.getProperty("java.vm.name"));//Java的虚拟机
        map.put("osName",sysProperty.getProperty("os.name"));//操作系统
        map.put("user_number",userMapper.user_number());
        MongoCollection<Document> task_number =  mongoTemplate.getCollection("task");
        map.put("task_number",task_number.count());
        MongoCollection<Document> comment_number =  mongoTemplate.getCollection("comment");
        MongoCollection<Document> subreview_number =  mongoTemplate.getCollection("subreview");
        map.put("comment_number",comment_number.count()+subreview_number.count());
        map.put("sms_number",smsMapper.sms_number());
        MongoCollection<Document> task_notice_number =  mongoTemplate.getCollection("taskNoticeStatusMsg");
        MongoCollection<Document> loveNotice_number =  mongoTemplate.getCollection("loveNotice");
        map.put("task_notice_number",task_notice_number.count());
        map.put("loveNotice_number",loveNotice_number.count());
        return new ModelAndView("/welcome",map);
    }

}
